/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.OneCommerceProject.charts;

/**
 *
 * @author Malek
 */
import com.OneCommerceProject.beans.ItemFacade;
import com.OneCommerceProject.beans.PrixFacade;
import com.OneCommerceProject.controllers.ItemController;
import com.OneCommerceProject.entities.Item;
import com.OneCommerceProject.entities.Prix;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import org.primefaces.model.chart.CartesianChartModel;
import org.primefaces.model.chart.ChartSeries;
import org.primefaces.model.chart.LineChartSeries;
import org.primefaces.model.chart.PieChartModel;

@ManagedBean(name = "charts")
@SessionScoped
public class charts {

    private CartesianChartModel categoryModel = new CartesianChartModel();
    private PieChartModel livePieModel = new PieChartModel();;
    @EJB
    private com.OneCommerceProject.beans.ItemFacade itemFacade;
    @EJB
    private com.OneCommerceProject.beans.PrixFacade prixFacade;
    private CartesianChartModel linearModel;

    public CartesianChartModel getLinearModel() {
        createLinearModel();
        return linearModel;
    }

    public void setLinearModel(CartesianChartModel linearModel) {
        this.linearModel = linearModel;
    }
    
//    public CartesianChartModel getCategoryModel() {
//        createCategoryModel();
//        return categoryModel;
//    }

    public PieChartModel getLivePieModel() {
        
        List<Item> itemList = itemFacade.findAll();
        for(Item current : itemList){
            livePieModel.getData().put(current.getNomitem(), current.getQuantiteitem());
        }
        return livePieModel;
    }


//    private void createCategoryModel() {
//
//        Item item = itemFacade.findByIdItem(new BigDecimal("54"));
//        List<Prix> listPrix = prixFacade.findByItem(item);
//
////        ChartSeries items = new ChartSeries();
//
////        items.setLabel("Quantité Items");
//                
//        for(Prix prix:listPrix){
//            categoryModel.set(prix.getDateprix().toString(),prix.getSomme().toBigInteger());
//        }
//
//        categoryModel.set(new Date().toString(), 1000);
////        items.set(new Date().toString(), 2000);
////        categoryModel.set(item, null);
//    }

    private void createLinearModel() {
        linearModel = new CartesianChartModel();

        LineChartSeries series1 = new LineChartSeries();
        series1.setLabel("Series 1");
        series1.setMarkerStyle("diamond");

//        series1.set(1, 2);
//        series1.set(2, 1);
//        series1.set(3, 3);
//        series1.set(4, 6);
//        series1.set(5, 8);

        List<Prix> listPrix = prixFacade.findByItem(itemFacade.findByIdItem(new BigDecimal(54)));

                
        for(Prix prix:listPrix){
            System.out.println(prix.getDateprix().toString());
            series1.set(prix.getDateprix().toString(),prix.getSomme());
        }
//        LineChartSeries series2 = new LineChartSeries();
//        series2.setLabel("Series 2");
//        series2.setMarkerStyle("diamond");
//
//        series2.set(1, 6);
//        series2.set(2, 3);
//        series2.set(3, 2);
//        series2.set(4, 7);
//        series2.set(5, 9);
        linearModel.addSeries(series1);
//        linearModel.addSeries(series2);
    }
}
